# coding:utf-8
from django.db.models import Q
from rest_framework import viewsets
from MSB_ERP.utils.base_views import MultipleDestroyMixin, MultipleOpenMixin
from MSB_ERP.utils.pagination import GlobalPagination
from basic_info.models import WarehouseModel, SettlementAccountModel
from basic_info.serializer.warehouse_Serializer import WarehouseSerializer, WarehouseSearchSerializer
from drf_yasg import openapi
from drf_yasg.utils import swagger_auto_schema
from rest_framework import status
from rest_framework.decorators import action

from rest_framework import serializers
from goods_info.models import GoodsCategoryModel
from goods_info.serializer.category_serializer import GoodsCategorySerializer


class CategoryView(viewsets.ModelViewSet, MultipleDestroyMixin):
    """
    create:
    货品类别--新增

    货品类别新增, status: 201(成功), return: 新增货品类别信息

    destroy:
    货品类别--删除

    货品类别删除, status: 204(成功), return: None

    multiple_delete:
    货品类别--批量删除,必传参数：ids=[1,2,3,4...]

    货品类别批量删除, status: 204(成功), return: None

    update:
    货品类别--修改,

    货品类别修改, status: 200(成功), return: 修改后的货品类别信息

    partial_update:
    货品类别--局部修改,可以传参任意属性的值，服务器会修改指定的属性值

    货品类别局部修改, status: 200(成功), return: 修改后的货品类别信息

    list:
    货品类别--获取类别列表，可选get请求参数:pid，如果没有传，则查询顶级类别，否则查询指定pid下的子类别

    货品类别列表信息, status: 200(成功), return: 货品类别信息列表

    retrieve:
    查询某一个货品类别

    查询指定ID的货品类别, status: 200(成功), return: 用户货品类别
    """

    queryset = GoodsCategoryModel.objects.all()
    serializer_class = GoodsCategorySerializer
   # pagination_class = GlobalPagination
    def get_queryset(self):
        if self.action == 'list':  # 可能需要过滤查询,判断是否查询顶级类别还是某个子类别

            # 获取请求参数(在get中)：pid
            pid = self.request.query_params.get('pid', None)
            if pid and pid != '0':

                return GoodsCategoryModel.objects.filter(parent__id=int(pid)).all()
            return GoodsCategoryModel.objects.filter(parent__isnull=True).all()
        else:
            return GoodsCategoryModel.objects.all()

    pid_param = openapi.Parameter(name='pid', in_=openapi.IN_QUERY, description='父类别的ID，可以不传，查询所有顶级类别',
                                  type=openapi.TYPE_INTEGER, )

    @swagger_auto_schema(
        manual_parameters=[pid_param],
        operation_description="商品类别列表")
    def list(self, request, *args, **kwargs):
        return super(CategoryView, self).list(request=request, *args, **kwargs)
